package holdobject;

import java.util.Collections;
import java.util.*;
import java.util.Random;
import typeinfo.pets.*;

import static net.mindview.util.Print.print;


public class ListFeatures {
    public static void main(String[] args) {
        Random random=new Random(47);
        List<Pet> pets=Pets.arrayList(7);//静态的Pets.arrayList()方法将返回一个填冲随
        print("1:"+pets);//机选取的Pet对象
        Hamster h=new Hamster();
        pets.add(h);//把仓鼠加到这个集合里
        print("2:"+pets);
        print("3:"+pets.contains(h));//判断列表中是否包含h仓鼠，包含是true，不包含返回false
        pets.remove(h);//删掉仓鼠
        Pet pe=pets.get(2);//选区列表里的第二个元素作为对象
        print("4:"+pe+" "+pets.indexOf(pe));//返回第一次出现指定元素的位置
        Pet cymric=new Cymric();//创建一个cymric的实例对象
        print("5:"+pets.indexOf(cymric));
        print("6:"+pets.remove(cymric));
        print("7:"+pets.remove(pe));
        print("8:"+pets);
        pets.add(3,new Mouse());
        print("9:"+pets);//集合pets
        List<Pet> sub=pets.subList(1,4);
        print("subList:"+sub);//pets的子集合
        print("10:"+pets.containsAll(sub));
        Collections.sort(sub);
        print("sorted subList:"+sub);
        //
        print("11:"+pets.containsAll(sub));
        Collections.shuffle(sub,random);
        print("shuffled subList"+sub);
        print("12:"+pets.containsAll(sub));

        List<Pet> copy=new ArrayList<Pet>(pets);//将pets,复制一份
        print(pets);
        sub=Arrays.asList(pets.get(1),pets.get(4));
        print("sub:"+sub);
        copy.retainAll(sub);
        print("13:"+copy);
        copy=new ArrayList<>(pets);
        copy.remove(2);
        print("14copy:"+copy);
        copy.removeAll(sub);
        print("15copy:"+copy);
        copy.addAll(2,sub);
        print("17:"+copy);

        print("18:"+pets.isEmpty());//判断集合是否为空
        pets.clear();
        print("19pets"+pets);
        print("20:"+pets.isEmpty());//isempty返回true或者false
        pets.addAll(Pets.arrayList(4));
        print(pets);
        Object[] o=pets.toArray();//返回所有元素的数组
        print("22:"+o[3]);



    }
}
